package com.sls.web.manager.game.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.SnnsOrderBalanceInfo;
import com.sls.web.manager.game.querybean.SnnsOrderBalanceQuery;
import com.sls.web.manager.game.service.SnnsOrderBalanceService;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 
 * @description 7996订单结果查询service
 * @author Wangxuegang
 * @date 2013-4-24
 */
@Repository("snnsOrderBalanceService")
public class SnnsOrderBalanceServiceImpl extends SuperService implements
		SnnsOrderBalanceService {

	private Gson gson;

	public SnnsOrderBalanceServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 订单结果查询
	 * 
	 * @param snnsOrderQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public SnnsOrderBalanceInfo queryOrder(
			SnnsOrderBalanceQuery snnsOrderQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_QUERY_ORDER);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, snnsOrderQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				SnnsOrderBalanceInfo snnsOrder = gson.fromJson(body,
						SnnsOrderBalanceInfo.class);
				return snnsOrder;
			} else {
				throw new ManagerException("未知错误！");
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 商户编号查询
	 * 
	 * @param snnsOrderBalanceInfo
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<SnnsOrderBalanceInfo> querySupUserId(
			SnnsOrderBalanceInfo snnsOrderBalanceInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_USER_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			List<SnnsOrderBalanceInfo> orderBalanceInfo = gson.fromJson(
					dto.getBody(), new TypeToken<List<SnnsOrderBalanceInfo>>() {
					}.getType());
			return orderBalanceInfo;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 7996余额查询
	 * 
	 * @param SnnsOrderBalanceQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public SnnsOrderBalanceInfo querySnnsBalance(
			SnnsOrderBalanceQuery snnsOrderBalanceQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_QUERY_BALANCE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, snnsOrderBalanceQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				SnnsOrderBalanceInfo orderBalanceInfo = gson.fromJson(
						dto.getBody(), SnnsOrderBalanceInfo.class);
				return orderBalanceInfo;
			} else {
				throw new ManagerException("未知错误！");
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
